/*
 * Copyright (c) 2002-2007, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */
package fr.paris.lutece.plugins.customisation.web;

import fr.paris.lutece.plugins.customisation.business.CustomisationPortletAvalaible;
import fr.paris.lutece.plugins.customisation.business.CustomisationPortletAvalaibleHome;
import fr.paris.lutece.plugins.customisation.web.constants.CustomisationParameters;
import fr.paris.lutece.portal.business.portlet.PortletType;
import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
import fr.paris.lutece.portal.business.role.Role;
import fr.paris.lutece.portal.business.role.RoleHome;
import fr.paris.lutece.portal.service.template.AppTemplateService;
import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
import fr.paris.lutece.util.ReferenceList;
import fr.paris.lutece.util.html.HtmlTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


/**
 * This class represents user interface Customisation. It is the base class of all user interface customisation.
 */
public class CustomisationJspBean extends PluginAdminPageJspBean
{
    // Right
    public static final String RIGHT_MANAGE_CUSTOMISATION = "CUSTOMISATION_MANAGEMENT_PORTLETS";

    // Templates files path
    private static final String TEMPLATE_MANAGE_CUSTOMISATION_PORTLETS = "admin/plugins/customisation/manage_customisation.html";
    private static final String TEMPLATE_MODIFY_CUSTOMISATION_PORTLETS = "admin/plugins/customisation/modify_customisation.html";

    // Markers
    private static final String MARK_CUSTOMISATION_LIST_TYPE_PORTLET = "customisation_list_type_portlet";
    private static final String MARK_CUSTOMISATION_LIST_ROLE = "customisation_list_role";
    private static final String MARK_CUSTOMISATION_CURRENT_ROLE = "current_role";
    private static final String MARK_CUSTOMISATION_ROLE = "one_role";
    private static final String MARK_CUSTOMISATION_LIST_TYPE_PORTLET_AVAILABLE = "customisation_list_type_portlet_available";

    // Properties for page titles
    private static final String PROPERTY_PAGE_TITLE_MANAGE_CUSTOMISATION = "customisation.manage_customisation.listAuthorizedTitle";
    private static final String PROPERTY_PAGE_TITLE_MODIFY_CUSTOMISATION = "customisation.modify_customisation.pageTitle";

    // Parameters
    private static final String PARAMETER_PORTLET_TYPE_AUTHORIZED = "portlet_type_authorized";
    private static final String PARAMETER_ROLE_KEY = "role_key";

    // Constants
    private String _strCurrentRole;

    /**
     * Returns customisation management form
     *
     * @param request The Http request
     * @return Html form
     */
    public String getManageCustomisation( HttpServletRequest request )
    {
        setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_CUSTOMISATION );

        String strAction = request.getParameter( CustomisationParameters.CUSTOMISATION_ACTION_ACTION );

        HashMap<String, Object> model = new HashMap<String, Object>(  );
        List<CustomisationPortletAvalaible> listPortletTypeSelected = new ArrayList<CustomisationPortletAvalaible>(  );
        List<PortletType> listPortletType = new ArrayList<PortletType>(  );

        if ( strAction == null )
        {
            listPortletTypeSelected = CustomisationPortletAvalaibleHome.getPortletTypeByRole( CustomisationParameters.CUSTOMISATION_ROLE_KEY_NO_RESTRICTION );
            _strCurrentRole = CustomisationParameters.CUSTOMISATION_ROLE_KEY_NO_RESTRICTION;
        }
        else
        {
            if ( strAction.equals( CustomisationParameters.CUSTOMISATION_ACTION_FILTER ) )
            {
                listPortletTypeSelected = CustomisationPortletAvalaibleHome.getPortletTypeByRole( request.getParameter( 
                            PARAMETER_ROLE_KEY ) );
                _strCurrentRole = request.getParameter( PARAMETER_ROLE_KEY );
            }
        }

        for ( int i = 0; i < listPortletTypeSelected.size(  ); i++ )
        {
            PortletType portletType = PortletTypeHome.findByPrimaryKey( listPortletTypeSelected.get( i )
                                                                                               .getIdPortletType(  ) );
            listPortletType.add( portletType );
        }

        model.put( MARK_CUSTOMISATION_LIST_TYPE_PORTLET, listPortletType );

        ReferenceList listRole = RoleHome.getRolesList(  );
        model.put( MARK_CUSTOMISATION_LIST_ROLE, listRole );
        model.put( MARK_CUSTOMISATION_CURRENT_ROLE, _strCurrentRole );

        HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CUSTOMISATION_PORTLETS, getLocale(  ),
                model );

        return getAdminPage( template.getHtml(  ) );
    }

    /**
     * Displays the customisation's modification form
     *
     * @param request The http request
     * @return The html code for displaying the modification form
     */
    public String getModifyCustomisation( HttpServletRequest request )
    {
        setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CUSTOMISATION );

        HashMap<String, Object> model = new HashMap<String, Object>(  );
        List<PortletType> listPortletType = PortletTypeHome.getPortletTypesList( getLocale(  ) );
        List<CustomisationPortletAvalaible> listPortletTypeSelected = CustomisationPortletAvalaibleHome.getPortletTypeByRole( _strCurrentRole );

        model.put( MARK_CUSTOMISATION_LIST_TYPE_PORTLET, listPortletType );
        model.put( MARK_CUSTOMISATION_LIST_TYPE_PORTLET_AVAILABLE, listPortletTypeSelected );

        Role role = RoleHome.findByPrimaryKey( _strCurrentRole );
        model.put( MARK_CUSTOMISATION_ROLE, role );

        HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CUSTOMISATION_PORTLETS, getLocale(  ),
                model );

        return getAdminPage( template.getHtml(  ) );
    }

    /**
     * Processes customisation's modification
     *
     * @param request The http request
     * @return The Management jsp url
     */
    public String doModifyCustomisation( HttpServletRequest request )
    {
        String[] arrayAuthorized = request.getParameterValues( PARAMETER_PORTLET_TYPE_AUTHORIZED );

        CustomisationPortletAvalaibleHome.removePortletTypeAuthorizedByRole( _strCurrentRole );

        if ( arrayAuthorized != null )
        {
            for ( int i = 0; i < arrayAuthorized.length; i++ )
            {
                CustomisationPortletAvalaibleHome.createPortletTypeAuthorized( arrayAuthorized[i], _strCurrentRole );
            }
        }

        return getHomeUrl( request );
    }
}
